// pages/home/home.js
const app = getApp();
// var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
// var qqmapsdk;
const api = require('../../apis/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude:'',
    longitude: '',
    /**
     * 添加markers后置为true,地图开始渲染
     */
    hasMarkers:true,
    markers:[{}],
    // 站点marker,不用与渲染界面,只用于站点切换
    // stationMarkers:[{}]
    // polyline: [{
    //   points: [{
    //     latitude: '40.006822',
    //     longitude: '116.481451'
    //   }, {
    //     longitude: '116.487847',
    //     latitude: '40.002607'
    //   }, {
    //     longitude: '116.496507',
    //     latitude: '40.006103'
    //   }, {
    //     latitude: '40.002607',
    //     longitude: '116.587847',
    //   }],
    //   width: 2,
    //   color: '#000000AA',
    //   dottedLine: false
    // }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // qqmapsdk = new QQMapWX({
    //   key: 'DIYBZ-CFKR2-Q33UM-CW4XB-LIQJ7-4AFNH'
    // });
    var _this=this;
    
    api.listGateWayByUid(function (res) {
      // console.log(res)
      var markersList = new Array();
      for (var index in res.data.datas) {
        var markerObject = { id: "", longitude: "", latitude: "", iconPath: "", objType: "gateWay", obj: null };

        markerObject.id = res.data.datas[index].id;
        markerObject.longitude = res.data.datas[index].lng;
        markerObject.latitude = res.data.datas[index].lat;
        markerObject.obj = res.data.datas[index];
        markerObject.iconPath = "../../img/location.png";

        markersList.push(markerObject);
      }

      _this.setData({
        markers: markersList, latitude: res.data.datas[0].lat,
        longitude: res.data.datas[0].lng, hasMarkers: true
      });
    });

    /**
     * 请求用户站点详情
     */
    // api.listStationByUid(function(res){
    //   var markersList = new Array();
    //   for (var index in res.data.datas) {
    //     var markerObject = { id: "", longitude: "", latitude: "", iconPath: "", objType: "station", obj: null };
   
    //     markerObject.id = res.data.datas[index].id;
    //     markerObject.longitude = res.data.datas[index].lng;
    //     markerObject.latitude = res.data.datas[index].lat;
    //     markerObject.obj = res.data.datas[index];
    //     markerObject.iconPath = "../../img/location.png";

    //     markersList.push(markerObject);
    //   }
    
    //   _this.setData({
    //     markers: markersList, latitude: res.data.datas[0].lat,
    //     longitude: res.data.datas[0].lng, hasMarkers:true});
    // });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  markerTab:function(e){
   
    var _this=this;
    let markers = _this.data.markers;// 获取markers列表
    // console.log("**** " + markers);
    let data=null;
    for (let item of markers) {
      if(item.id==e.markerId){
        // console.log("**** " + JSON.stringify(item));
        data=item;
        // console.log("id1= " + data.id);
        break;
      }
    };
    // console.log("  data= " + JSON.stringify(data));
    if (data==null){
      return;
    }
    // console.log("  type= " + data.objType);
    switch (data.objType){
      case "station":
        _this.requestStationDetail(_this, data.id);
        break;
      case "gateWay":
      console.log("跳转gateWay");
        wx.navigateTo({
          url: '../gateWay/gateWay?data=' + escape(JSON.stringify(data.obj))
        });
        break;
      case "collectorSingle":
        wx.navigateTo({
          url: '../collectorSingle/collectorSingle?data=' + escape(JSON.stringify(data.obj))
        });
        console.log("跳转collectorSingle");
        break;
    }

    
  },
  requestStationDetail:function(_this,id){
    api.requestStationDetail(id, function (res) {
      console.log(res);
      var markersList = new Array();
      if (res.data.gateWays != null) {
        for (var index in res.data.gateWays) {
          markersList.push(_this.drawGateWay(res.data.gateWays[index]));
        }
      }
      // console.log("collectorSingles = " + JSON.stringify(res.data.collectorSingles));
      if (res.data.collectorSingles != null) {
        for (var index in res.data.collectorSingles) {
          markersList.push(_this.drawCollectorSingle(res.data.collectorSingles[index]));
        }
      }
      // console.log("markersList = " + JSON.stringify(markersList));
      _this.setData({ markers: markersList });
      _this.data.hasMarkers = true;
    });
    //请求站点详情
    // wx.request({
    //   url: app.globalData.URL + "/station/listDetail/" + id,
    //   header: {
    //     "Accept": "application/json",
    //     "Content-Type": "application/json",
    //     "token": app.globalData.token
    //   },
    //   method: "GET",
    //   // data: { username: this.data.username, password: md5.hexMD5(this.data.password) },
    //   complete: function (res) {
    //     console.log(res);
    //     var markersList = new Array();
    //     if (res.data.gateWays != null) {
    //       for (var index in res.data.gateWays) {
    //         markersList.push(_this.drawGateWay(res.data.gateWays[index]));
    //       }
    //     }
    //     // console.log("collectorSingles = " + JSON.stringify(res.data.collectorSingles));
    //     if (res.data.collectorSingles != null) {
    //       for (var index in res.data.collectorSingles) {
    //         markersList.push(_this.drawCollectorSingle(res.data.collectorSingles[index]));
    //       }
    //     }
    //     // console.log("markersList = " + JSON.stringify(markersList));
    //     _this.setData({ markers: markersList });
    //     _this.data.hasMarkers = true;
    //   }
    // });
  },

  /**
   * 初始化网关
   */
  drawGateWay:function(data){
    
    var markerObject = { id: "", longitude: "", latitude: "", iconPath: "", objType: "gateWay", obj: data };
      markerObject.id = data.id;
      markerObject.longitude = data.lng;
      markerObject.latitude = data.lat;
      markerObject.iconPath = "../../img/gate_way.png";
    
    return markerObject;

  },
  /**
   * 初始化单采集器
   */
  drawCollectorSingle: function (data) {
    var markerObject = { id: "", longitude: "", latitude: "", iconPath: "", objType: "collectorSingle", obj: data};
    markerObject.id = data.id;
    markerObject.longitude = data.lng;
    markerObject.latitude = data.lat;
    markerObject.iconPath = "../../img/collector.png";
    return markerObject;
  },
  /**
   * 初始化摄像头
   */
  drawCamera: function (data) {

  },
  /**
   * 初始化水肥一体机
   */
  drawFertilizer: function (data) {

  },
  /**
   * 绘制站点区域
   */
  drawFertilizer: function (data) {

  }

})